<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :keyboard="false"
    v-bind:prefixNo="prefixNo"
    switchHelp
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    wrapClassName="ant-modal-cust-warp"
    :id="prefixNo"
    style="top:5%;height: 100%;overflow-y: hidden">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="24">
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工程名称" data-step="1" data-title="工程名称">
              <a-select placeholder="选择工程名称" v-decorator.trim="[ 'engineerId', validatorRules.engineerId]"  :dropdownMatchSelectWidth="false"
                  showSearch optionFilterProp="children" @change="changeSelect">
                 <a-select-option v-for="(item,index) in engineerList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="送货地址">
                <a-input placeholder="请输入送货地址" v-decorator.trim="[ 'address' ]" />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="发货车" >
              <a-input placeholder="请输入发货车" v-decorator.trim="[ 'car' ]" />
            </a-form-item>
          </a-col>
           <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="发货司机" >
              <a-input placeholder="请输入发货司机" v-decorator.trim="[ 'driver' ]" />
            </a-form-item>
          </a-col>
           <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="发货批次" >
              <a-input placeholder="" v-decorator.trim="[ 'batch' ]" :read-only="true" />
            </a-form-item>
          </a-col>
        </a-row>
        <j-editable-table id="deliveryModal"
          :ref="refKeys[0]"
          :loading="materialTable.loading"
          :columns="materialTable.columns"
          :dataSource="materialTable.dataSource"
          :maxHeight="300"
          :rowNumber="false"
          :rowSelection="true"
          :actionButton="true"
          :dragSort="true"
          @valueChange="onValueChange"
          @deleted="onDeleted">
        </j-editable-table>
    
        <a-row class="form-row" :gutter="24">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="{xs: { span: 24 },sm: { span: 24 }}" label="">
              <a-textarea :rows="3" placeholder="请输入备注" v-decorator="[ 'description' ]" style="margin-top:8px;"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import { FormTypes } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { DeliveryModalMixin } from '../mixins/DeliveryModalMixin'
  import { handleIntroJs } from "@/utils/util"
  import JUpload from '@/components/jeecg/JUpload'
  import JDate from '@/components/jeecg/JDate'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import Vue from 'vue'
  export default {
    name: "DeliveryModal",
    mixins: [JEditableTableMixin,DeliveryModalMixin],
    components: {
      JUpload,
      JDate,
      JSelectPosition
    },
    data () {
      return {
        title:"操作",
        width: '1600px',
        moreStatus: false,
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        visible: false,
        supList: [],
        depotList: [],
        operTimeStr: '',
        prefixNo: 'CGDD',
        fileList:[],
        produceList:[],
        model: {},
        engineerId:"",
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        refKeys: ['materialDataTable', ],
        tableKeys:['materialDataTable', ],
        activeKey: 'materialDataTable',
        materialTable: {
          loading: false,
          dataSource: [],
          columns: [
            { title: '产品名称', key: 'produceId', width: '7%', type: FormTypes.select, placeholder: '请选择${title}', options: [],
              allowSearch:true, validateRules: [{ required: true, message: '${title}不能为空'}]
            },
            { title: '规格(宽/mm)', key: 'width', width: '10%', type: FormTypes.normal,
            validateRules: [{ required: true, message: '${title}不能为空' }]},
            { title: '规格(高/mm)', key: 'height', width: '10%', type: FormTypes.normal,
            validateRules: [{ required: true, message: '${title}不能为空' }]},
            { title: '规格(长/mm)', key: 'length', width: '10%', type: FormTypes.normal,
            validateRules: [{ required: false }]},
            { title: '单位', key: 'unit', width: '10%', type: FormTypes.normal,
              validateRules: [{ required: true, message: '${title}不能为空' }]},
            { title: '发货数量', key: 'number', width: '10%', type: FormTypes.inputNumber,statistics: true,
              validateRules: [{ required: true, message: '${title}不能为空' }]},
            { title: '单价（/元）', key: 'price', width: '10%', type: FormTypes.inputNumber,
              validateRules: [{ required: true, message: '${title}不能为空' }]},
            { title: '合价（/元）', key: 'total', width: '10%', type: FormTypes.inputNumber, defaultValue: '', statistics: true,
              validateRules: [{ required: true, message: '${title}不能为空' }]},
          ]
        },
        confirmLoading: false,
        validatorRules:{
        },
        url: {
          add: '/delivery/addDeliveryAndItem',
          edit: '/delivery/updateDeliveryAndItem',
          detailList: '/deliveryItem/getDetailList',
          produceList: '/engineerProduce/getListByEngineerId'
        }
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token};
   
    },
    methods: {
      //调用完edit()方法之后会自动调用此方法
      editAfter() {
        if (this.action === 'add') {
          this.addInit(this.prefixNo)
          this.fileList = []
          this.$nextTick(() => {
            handleIntroJs(this.prefixNo, 1)
          })
        } else {
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model,'engineerId', 'address','car','driver','batch','description'))
          });
          // 加载子表数据
          let params = {
            headerId: this.model.id,
            name: this.model.name,
            engineerId:this.model.engineerId
          }
          let url = this.readOnly ? this.url.detailList : this.url.detailList;
          this.requestSubTableData(url, params, this.materialTable);
        }
        let params = {
            type: 'delivery',
            engineerId:this.model.engineerId
          }
         this.initEngineer(params);
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let billMain = Object.assign(this.model, allValues.formValue)
        let detailArr = allValues.tablesValue[0].values
        if(this.model.id){
          billMain.id = this.model.id
        }
        return {
          info: JSON.stringify(billMain),
          rows: JSON.stringify(detailArr),
        }
      },
      changeSelect(value) {
        console.log(value,'value');
        let that = this;
          for (var i = 0; i <this.engineerList.length; i++) {
           var oneSubject=this.engineerList[i]
            if(value===oneSubject.id){
              let arr = oneSubject.produceName
               for(let item of that.materialTable.columns){
                console.log(item,'item');
                  if(item.key == 'produceId' ) {
                    item.options = []
                    for(let i=0; i<arr.length; i++) {
                      let depotInfo = {};
                      depotInfo.value = arr[i].id  //注意-此处value必须为字符串格式-
                      depotInfo.text = arr[i].name
                      depotInfo.title = arr[i].name
                      item.options.push(depotInfo)
                    }
               }
             }
              this.form.setFields({ batch : {value:oneSubject.batch}});
            }
          }
      }
    }
      
  }
</script>
<style scoped>

</style>